<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CreateReceivingOrder
 *
 * @author Administrator
 */
class Stock extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('/wms/inventory_service');
        $this->load->service('/wms/inventory_on_order_service');
        $this->load->service('wms/api_service');
    }

    function out_stock()
    {
        $gp = json_decode(file_get_contents('php://input'), true);
        $authorized = $this->api_service->checkAuthorized($gp);
        $lstProduct = $gp['data'];
        $skuArray = array();
        foreach ($lstProduct as $productInfo)
        {
            $one['sku'] = $productInfo['sku'];
            $one['whsCode'] = $productInfo['whsCode'];
            $one['quantity'] = $productInfo['quantity'];
            $one['refNum'] = $productInfo['orderId'];
            $one['comments'] = $productInfo['comments'];
            $skuArray[] = $one;
        }
        $rs = $this->inventory_service->out_stock_array_t($skuArray);
        echo json_encode($rs);
    }

    function release()
    {
        $gp = gp();
        $authorized = $this->api_service->checkAuthorized($gp);
        $parts_list = $gp['data']['parts_list'];
        //查询是否曾经锁定库存
        $where = array(
            'order_id'=>$parts_list[0]['order_num'] . '-' . $parts_list[0]['pid']
        );
        
        $rs = $this->inventory_on_order_service->get_list_count($where);
        if(!$rs){
            echo '{"success":false,"msg":"release false 1"}';
            exit;
        }
        $rs = $this->inventory_on_order_service->clean_onorder_parts_list_t($parts_list);
        if ($rs)
        {
            echo '{"success":true,"msg":""}';
        }
        else
        {
            echo '{"success":false,"msg":"release false"}';
        }
    }

    function lock()
    {
        $gp = gp();
        $authorized = $this->api_service->checkAuthorized($gp);
        $parts_list = $gp['data']['parts_list'];
        $product_first = $gp['data']['product_first'];
        $merchant_num = $product_first['brandCustomerNum'];
        $warranty = $product_first['inWarranty'];
        foreach ($parts_list as $value)
        {
           if($value['whs_code']=='' or $value['whs_code']==null){
                           echo '{"success":true,"msg":""}';
               exit;
           }
        }
        $rs = $this->inventory_on_order_service->set_onorder_parts_list_t($parts_list, $merchant_num, $warranty, $in_or_out = 'out');
        if ($rs)
        {
            echo '{"success":true,"msg":""}';
        }
        else
        {
            echo '{"success":false,"msg":"lock false"}';
        }
    }

}

?>
